package com.zmkj.service.Imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zmkj.dao.SysCourseDao;
import com.zmkj.dao.SysCourseUserDao;
import com.zmkj.domain.SysCourse;
import com.zmkj.domain.SysCourseUser;
import com.zmkj.domain.SysUser;
import com.zmkj.service.SysCourseService;
import com.zmkj.utils.Code;
import com.zmkj.utils.Rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.List;


@Service
public class SysCourseServiceImp implements SysCourseService {
    @Autowired
    private SysCourseUserDao sysCourseUserDao;
    @Autowired
    private SysCourseDao sysCourseDao;


    @Override
    public Rest GetCourseData(Integer id) {
        LambdaQueryWrapper<SysCourseUser> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.eq(SysCourseUser::getUserId,id);
        List<SysCourseUser> sysCourseUsersList = sysCourseUserDao.selectList(courseLambdaQueryWrapper);
        ArrayList<SysCourse> sysCourses = new ArrayList<>();
        for(SysCourseUser courseuser : sysCourseUsersList){
            LambdaQueryWrapper<SysCourse> courseWrapper = new LambdaQueryWrapper<>();
            courseWrapper.eq(SysCourse::getId,courseuser.getCourseId());
            SysCourse sysCourseData = sysCourseDao.selectOne(courseWrapper);
            sysCourses.add(sysCourseData);
        }
        return new Rest(Code.rc200.getCode(),sysCourses,Code.rc200.getMsg());
    }

    @Override
    public Rest DeleteCourse(Integer courseId,Integer userId) {
        LambdaQueryWrapper<SysCourseUser> sysCourseUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysCourseUserLambdaQueryWrapper.eq(SysCourseUser::getCourseId,courseId).eq(SysCourseUser::getUserId,userId);
        int delete = sysCourseUserDao.delete(sysCourseUserLambdaQueryWrapper);
        return new Rest(Code.rc200.getCode(),"该课程已移除");
    }
}
